package com.zadtek.shopmall.other;

import android.content.Context;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zack.appupdate.AppUpdate;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.bean.GetBoundsResponse;
import com.zadtek.shopmall.bean.OperateResponseBean;
import com.zadtek.shopmall.bean.RedPacketBean;
import com.zadtek.shopmall.bean.UpdateVersionBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.dialog.BaseNiceDialog;
import com.zadtek.shopmall.view.dialog.NiceDialog;
import com.zadtek.shopmall.view.dialog.ViewConvertListener;
import com.zadtek.shopmall.view.dialog.ViewHolder;

import java.io.File;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class DialogManger {
    private static NiceDialog LoadingDialog;
    private static NiceDialog updialog;

    public static void showRedDialog(FragmentManager manager, final RedPacketBean bean){
        final NiceDialog niceDialog = NiceDialog.init();
        niceDialog.setLayoutId(R.layout.red_dialog)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder var1, BaseNiceDialog var2) {
                        var1.setOnClickListener(R.id.close, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                niceDialog.dismiss();
                            }
                        });
                        var1.setText(R.id.price, String.valueOf(bean.getBonusamount()));
                        var1.setText(R.id.condition,"满"+bean.getMinus_amount()+"可用");
                        var1.setText(R.id.desc,bean.getValid_time());
                    }
                }).setOutCancel(false);
        niceDialog.show(manager);
    }

    public static void showRedListDialog(final AppCompatActivity context, final GetBoundsResponse boundsResponse){
        final NiceDialog niceDialog = NiceDialog.init();
        niceDialog.setLayoutId(R.layout.red_dialog_list)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder var1, BaseNiceDialog var2) {
                        RecyclerView recycleview = (RecyclerView) var1.getView(R.id.list);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                        recycleview.setLayoutManager(linearLayoutManager);
                        Adapter adapter = new Adapter(R.layout.red_packet_item,boundsResponse.getBonus_list());
                        recycleview.setAdapter(adapter);
                        var1.setOnClickListener(R.id.close, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Map<String, Object> param = NetTool.newParams();
                                param.put("bonus_ids",boundsResponse.getBonus_ids());
                                NetTool.getApi().takebonus(param).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(new BaseObserver<BaseResponse<OperateResponseBean>>() {
                                            @Override
                                            public void onData(BaseResponse<OperateResponseBean> value) {
                                                if(value.getCode() == 0 && value.getData().isSuccess()){
                                                    ToastUtil.showToast("领取红包成功！");
                                                }else{
                                                    ToastUtil.showToast("领取红包失败！");
                                                }
                                            }
                                        });
                                niceDialog.dismiss();
                            }
                        });
                        Glide.with(context).load(boundsResponse.getBonus_backimg()).into((ImageView) var1.getView(R.id.iv_img));
                    }
                })
                .setMargin(40)
                .setOutCancel(false);
        niceDialog.show(context.getSupportFragmentManager());
    }

    private static class Adapter extends BaseQuickAdapter<RedPacketBean,BaseViewHolder>{
        public Adapter(int layoutResId, @Nullable List<RedPacketBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, RedPacketBean item) {
            helper.setText(R.id.tv_value,String.valueOf(item.getBonusamount()));
            helper.setText(R.id.tv_title,item.getDesc());
            helper.setText(R.id.tv_desc,item.getValid_time());
        }
    }

    public static void  showLoadingProgress(FragmentManager fragmentManager, final String text) {
        if (LoadingDialog == null && updialog ==null) {
            LoadingDialog = NiceDialog.init()
                    .setLayoutId(R.layout.loading);
            if(!TextUtils.isEmpty(text)){
                LoadingDialog.setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder var1, BaseNiceDialog var2) {
                        var1.setText(R.id.text,text);
                    }
                });
            }
            LoadingDialog.setWidth(100)
                    .setHeight(100)
                    .setDimAmount(0)
                    .setOutCancel(false)
                    .show(fragmentManager);
        }
    }

    public static void dismissLoadingProgress() {
        if(LoadingDialog!=null){
            LoadingDialog.dismiss();
            LoadingDialog = null;
        }
    }

    public static void showUpdate(final AppCompatActivity context, final UpdateVersionBean bean){
        updialog = NiceDialog.init();
        updialog.setLayoutId(R.layout.update_dialog)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder var1, BaseNiceDialog var2) {
                        var1.setOnClickListener(R.id.btn, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                updialog.dismiss();
                                update(context,bean.getAddress());
                            }
                        });
                        var1.setText(R.id.title, bean.getVersion_number());
                        var1.setText(R.id.desc,bean.getDsc());
                    }
                }).setMargin(60)
                .setOutCancel(true);
        updialog.show(context.getSupportFragmentManager());
    }

    private static void update(Context context,String url) {
        String dirFilePath = "";
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            //SD卡有用
            dirFilePath = context.getExternalFilesDir("apk/dianba.apk").getAbsolutePath();
        }else{
            //SD卡没有用
            dirFilePath = context.getFilesDir()+ File.separator+"apk/dianba.apk";
        }
        AppUpdate.init(context)
                .setSavePath(dirFilePath)
                .setDownloadUrl(url)
                .download();
    }
}
